/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.listener;

import java.util.*;

/*******************************************************************************
 * This thread monitors nfse.DataSockets and periodically calls
 * nfse.DataSocket.purgeRecords() to avoid starvation of low-volume data
 * streams.
 * 
 */
public class NetListenerSocketMonitor extends Thread {

	/***************************************************************************
	 * The interval to call DataSocket.purgeRecords().
	 */
	int seconds = 300;

	/***************************************************************************
	 * The DataSockets instantiated by the nfse.DataServer associated with this
	 * thread.
	 */
	Vector<NetListenerSocket> netListenerSockets = new Vector<NetListenerSocket>();

	/***************************************************************************
	 * Basic constructor sets the seconds attribute.
	 * 
	 * @param seconds
	 *            Determines how often to check the nfse.DataSocket's record
	 *            buffer.
	 * 
	 */
	public NetListenerSocketMonitor(int seconds) {
		this.seconds = seconds;
	}

	/***************************************************************************
	 * Adds a nfse.DataSocket to monitor. Sockets are removed by chechSockets()
	 * when they are found to be closed/inactive.
	 * 
	 * @param ds
	 *            The nfse.DataSocket to add.
	 */
	public synchronized void addNetListenerSocket(NetListenerSocket ds) {
		netListenerSockets.addElement(ds);
	}

	/***************************************************************************
	 * For each nfse.DataSocket being monitored, check to see if it is still
	 * active. If not, remove it from the list. Else call purgeRecords().
	 */
	public synchronized void checkSockets() {
		for (int i = 0; i < netListenerSockets.size(); i++) {
			NetListenerSocket ds = (NetListenerSocket) netListenerSockets
					.elementAt(i);
			if (!ds.isAlive()) {
				netListenerSockets.removeElementAt(i);
				i--;
			} else if ((System.currentTimeMillis() - ds.getLastCalled()) / 1000 >= seconds) {
				try {
					ds.closeFile(ds.getCurrentCount());
					ds.nextFile();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	/***************************************************************************
	 * Runs forever, periodically calling purgeRecords() on nfse.DataSockets in
	 * the dataSockets Vector.
	 */
	public void run() {

		while (true) {
			try {

				// Record the start time
				long start = System.currentTimeMillis();

				checkSockets();

				// Record the end time
				long end = System.currentTimeMillis();

				// Sleep for any amount of time remaining
				if (end - start < seconds * 1000) {
					Thread.sleep(5000);
				} else {
					System.out
							.println("The checkSockets() call took longer than "
									+ seconds
									+ " seconds to execute. Consider increasing the seconds attribute, lowering the load to the server or upgrading to better hardware.");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

}